package com.example.plugin.web.result;

import com.example.plugin.web.enums.SystemEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class ApiResult<T> {

    private int code;
    private String msg;
    private T data;

    public ApiResult success() {
        return setEnum(SystemEnum.SUCCESS);
    }

    public ApiResult success(T data) {
        ApiResult apiResult = setEnum(SystemEnum.SUCCESS);
        apiResult.setData(data);
        return apiResult;
    }

    public ApiResult exception(SystemEnum systemEnum, T data) {
        this.code = systemEnum.getCode();
        this.msg = systemEnum.getMsg();
        this.data = data;
        return this;
    }

    public ApiResult exception(SystemEnum systemEnum) {
        return exception(systemEnum, null);
    }


    public ApiResult setEnum(SystemEnum systemEnum){
        this.code = systemEnum.getCode();
        this.msg = systemEnum.getMsg();
        return this;
    }

    public ApiResult exception(ApiException e) {
        this.code = e.getCode();
        this.msg = e.getMsg();
        return this;
    }

}
